﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.IO;

namespace backoffice
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {

                    string path = Server.MapPath("backoffceupload") + @"\" + FileUpload1.PostedFile.FileName;
                    if (System.IO.Path.GetExtension(this.FileUpload1.PostedFile.FileName).ToLower() == ".xls" ||
                        System.IO.Path.GetExtension(this.FileUpload1.PostedFile.FileName).ToLower() == ".xlsx")
                    {
                        FileUpload1.PostedFile.SaveAs(path);
                    }
                    if (File.Exists(path))
                    {
                        try
                        {
                            backofficecomponent comp = new backofficecomponent();
                             DataTable dt= comp.ExcelReader(path);
                             int NumberOfRows = comp.ExcelFileRowCount;
                             if (NumberOfRows > 0)
                             {
                                 backofficereport[] row = backofficereport.InitArray(comp.ExcelFileRowCount);
                                 for (int i = 1; i < comp.ExcelFileRowCount; i++)
                                 {
                                     row[i].CustomerId = 3; //get this from session or wherever it is stored
                                     row[i].Date = DateTime.Parse(dt.Rows[i]["F1"].ToString());
                                     row[i].TimeUnit = TimeSpan.Parse(dt.Rows[i]["F2"].ToString());
                                     row[i].CustumersForEachCashBox = Convert.ToInt32(dt.Rows[i]["F3"].ToString());
                                     row[i].CustomersTotal = Convert.ToInt32(dt.Rows[i]["F4"].ToString());
                                     row[i].InsertDb();
                                 }
                                 Response.Redirect("WebForm1.aspx");

                             }
                        }
                        catch (Exception er) {
                            Label2.Text = er.Message;
                        }
                    }
                    else
                    {
                        Label2.Text = "oops! bara Microsoft Excel Fil!";
                    }

                
            }
        }
    }
}
